package com.jasiri.project.utils.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.jasiri.project.R
import com.jasiri.project.app.App
import com.jasiri.project.databinding.DialogPermissionReasonBinding
import com.jasiri.project.databinding.ItemPermissionReasonChildBinding
import com.jasiri.project.pro.ProDialog
import com.jasiri.project.utils.event.EventType
import com.jasiri.project.utils.http.HttpHelper
import org.json.JSONObject

/**
 * Sikadwa
 * @Author wwdeng
 * @CreateTime: 2024/7/31 13:45
 * @Annotation：权限申明 dialog
 */
class PermissionReasonDialog: ProDialog {
    private lateinit var binding: DialogPermissionReasonBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    override fun initWindow() {
        val params = window?.attributes
        params?.width = App.getPhoneWidth() - com.jasiri.project.utils.DensityUtils.dp2px(32f)
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window?.setGravity(Gravity.CENTER)
    }

    constructor(context: Context) : super(context) {}

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogPermissionReasonBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(R.id.btnDeny)
        setOnClickListener(R.id.btnAgree)
    }

    override fun initData() {
    }

    /**
     * 设置提示文案
     */
    fun showSikadwaPermissionReason(info: JSONObject){
        binding.imgIcon.setImageResource(info.optInt("iconReason"))
        binding.textPermissionTitle.text = info.optString("titleReason").removeRange(0,2)

        binding.permissionDesLayout.removeAllViews()
        val msgList = info.optJSONArray("msgList")
        msgList?.let {
            for (i in 0..< it.length()){
                val item = it.optJSONObject(i)
                val itemView = ItemPermissionReasonChildBinding.inflate(layoutInflater)
                val showTitle = item.optString("title")
                if (showTitle.isNotEmpty()){
                    itemView.tvTitle.text = showTitle
                    itemView.tvTitle.visibility = View.VISIBLE
                } else {
                    itemView.tvTitle.visibility = View.GONE
                }
                itemView.tvDes.text = item.optString("des")
                binding.permissionDesLayout.addView(itemView.root)
            }
        }
    }

    override fun onViewClick(v: View?) {
        dismiss()
        when(v?.id){
            R.id.btnAgree -> {
                mCallback?.onPermissionAgreeClick()
            }
            else -> {
                mCallback?.onPermissionDenyClick()
            }
        }
    }

    interface IDialogCallback {
        //点击同意
        fun onPermissionAgreeClick()
        //点击拒绝
        fun onPermissionDenyClick()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_PERMISSION_REASON_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_PERMISSION_REASON_DISMISS,eventRemark)
    }
}